/// @tags: SegTree
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

namespace BlueQuantum {

typedef long long ll;

int const N = 1e5 + 2e4 + 5;

struct Query {
  int l, id;
};

struct Node {
  int tag, cnt, min;
  ll m_cnt, ans;
} tr[N << 2];

int n, q, maxtop, mintop;
int a[N], maxstk[N], minstk[N];
ll ans[N];
vector<Query> vec[N];

inline void pushup(int x) {
  int ls = x << 1, rs = x << 1 | 1;
  if (tr[ls].min < tr[rs].min) tr[x].min = tr[ls].min, tr[x].m_cnt = tr[ls].m_cnt;
  else if (tr[ls].min > tr[rs].min)
    tr[x].min = tr[rs].min, tr[x].m_cnt = tr[rs].m_cnt;
  else
    tr[x].min = tr[ls].min, tr[x].m_cnt = tr[ls].m_cnt + tr[rs].m_cnt;
  tr[x].ans = tr[ls].ans + tr[rs].ans;
}

inline void pushdown(int x) {
  if (tr[x].tag) {
    int ls = x << 1, rs = x << 1 | 1;
    tr[ls].min += tr[x].tag, tr[ls].tag += tr[x].tag;
    tr[rs].min += tr[x].tag, tr[rs].tag += tr[x].tag;
    tr[x].tag = 0;
  }
  if (tr[x].cnt) {
    int ls = x << 1, rs = x << 1 | 1;
    if (tr[ls].min == tr[x].min)
      tr[ls].ans += tr[ls].m_cnt * tr[x].cnt, tr[ls].cnt += tr[x].cnt;
    if (tr[rs].min == tr[x].min)
      tr[rs].ans += tr[rs].m_cnt * tr[x].cnt, tr[rs].cnt += tr[x].cnt;
    tr[x].cnt = 0;
  }
}

void modify(int x, int l, int r, int L, int R, int v) {
  if (L <= l && r <= R) {
    tr[x].min += v, tr[x].tag += v;
    return;
  }
  int mid = (l + r) >> 1;
  pushdown(x);
  if (L <= mid) modify(x << 1, l, mid, L, R, v);
  if (mid < R) modify(x << 1 | 1, mid + 1, r, L, R, v);
  pushup(x);
}

ll query(int x, int l, int r, int L, int R) {
  if (L <= l && r <= R) return tr[x].ans;
  int mid = (l + r) >> 1;
  pushdown(x);
  ll res = 0;
  if (L <= mid) res = query(x << 1, l, mid, L, R);
  if (mid < R) res += query(x << 1 | 1, mid + 1, r, L, R);
  return res;
}

void build(int x, int l, int r) {
  tr[x].min = l, tr[x].m_cnt = 1;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build(x << 1, l, mid);
  build(x << 1 | 1, mid + 1, r);
}

inline int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  cin >> q;
  Query tmp;
  for (int i = 1, r; i <= q; ++i) cin >> tmp.l >> r, tmp.id = i, vec[r].push_back(tmp);
  build(1, 1, n);
  for (int i = 1; i <= n; ++i) {
    modify(1, 1, n, 1, n, -1);
    while (maxtop && a[i] > a[maxstk[maxtop]])
      modify(1, 1, n, maxstk[maxtop - 1] + 1, maxstk[maxtop], a[i] - a[maxstk[maxtop]]),
          maxtop--;
    maxstk[++maxtop] = i;
    while (mintop && a[i] < a[minstk[mintop]])
      modify(1, 1, n, minstk[mintop - 1] + 1, minstk[mintop], a[minstk[mintop]] - a[i]),
          mintop--;
    minstk[++mintop] = i;
    tr[1].ans += tr[1].m_cnt, tr[1].cnt++;
    for (vector<Query>::iterator it = vec[i].begin(); it != vec[i].end(); ++it) {
      ans[it->id] = query(1, 1, n, it->l, i);
    }
  }
  for (int i = 1; i <= q; ++i) cout << ans[i] << '\n';
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("CF997E Good Subsegments.in", "r", stdin);
  freopen("CF997E Good Subsegments.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
